package com.onnext.androidsdk

import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.onnext.androidsdk.base.BaseActivity
import com.onnext.androidsdk.config.Names
import com.onnext.androidsdk.module.view.ViewFragment
import com.onnext.androidsdk.util.Go2Util
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        recyclerview.layoutManager = LinearLayoutManager(this)
        recyclerview.adapter = MainAdapter(applicationContext)
    }

    class MainAdapter : RecyclerView.Adapter<MainAdapter.MainHolder> {
        private var context:Context
        var items = hashMapOf<String,String>()
        var modules = arrayOf(items)
        constructor(context: Context) {
            this.context = context
            items[Names.ClassName] = ViewFragment::class.java.name
            items[Names.Title] = "View的事件体系"
        }
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int): MainHolder {
            var view = LayoutInflater.from(context).inflate(R.layout.list_main,parent,false)
            return MainHolder(view)
        }

        override fun getItemCount(): Int {
            return modules.size
        }

        override fun onBindViewHolder(holder: MainHolder, position: Int) {
            holder.title.text = modules[position][Names.Title]
            holder.itemView.setOnClickListener {
                when(position) {
                    0->Go2Util.goModule(context,modules[position])
                }
            }
        }

        class MainHolder: RecyclerView.ViewHolder {
            var title: TextView
            constructor(itemView: View) : super(itemView) {
                title = itemView.findViewById(R.id.title)
            }
        }
    }
 }
